package com.smile.service.impl;

import com.smile.request.AssociateRequest;
import com.smile.request.TranslateRequest;
import com.smile.exception.RequestException;
import com.smile.result.AssociateResponseResult;
import com.smile.result.TranslateResponseResult;
import com.smile.service.ITranslateService;
import com.smile.utils.ConvertParamUtils;
import com.smile.utils.FastJsonUtils;
import com.smile.utils.OkHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * <p>翻译服务类</p>
 *
 * @author hzhan
 */
@Slf4j
public class TranslateServiceImpl implements ITranslateService {

    /**
     * <p>翻译</p>
     *
     * @param request
     * @return
     */
    @Override
    public TranslateResponseResult doTramslate(TranslateRequest request) {
        checkParamsLegitimate(request.getW());
        log.debug("【 搜索参数 】------------->>>> keywork : {}", request.getW());
        Map requestMap = FastJsonUtils.stringToCollect(FastJsonUtils.toJSONString(request));
        String url = ConvertParamUtils.covertToUrlParams(requestMap);
        return OkHttpUtils.doSynGet(TRANSLATE_URL + url, TranslateResponseResult.class);
    }

    /**
     * <p>获取联想</p>
     *
     * @param request
     * @return
     */
    @Override
    public AssociateResponseResult getAssociate(AssociateRequest request) {
        checkParamsLegitimate(request.getWord());
        Map requestMap = FastJsonUtils.stringToCollect(FastJsonUtils.toJSONString(request));
        String url = ConvertParamUtils.covertToUrlParams(requestMap);
        return OkHttpUtils.doSynGet(ASSOCIATE_URL + url, AssociateResponseResult.class);
    }


    /**
     * <p>检查参数是否合法</p>
     *
     * @param keyWork
     */
    private void checkParamsLegitimate(String keyWork) {
        if (StringUtils.isBlank(keyWork)) {
            throw new RequestException("请求参数没有传入");
        }
    }

}
